package com.njcc.service.impl;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.List;

import com.njcc.po.PrintPrice;
import com.njcc.service.CountPriceService;
import com.njcc.service.GetFilePages;

/**
 *	实现CountPriceService计算文件价格的业务
 *
 */
public class CountPriceServiceImpl implements CountPriceService{
	
	PrintInfoServiceImpl pis = new PrintInfoServiceImpl();
	List<PrintPrice> p = pis.getPrice();	//得到各类型的打印价格
	DecimalFormat df = new DecimalFormat("######0.00");
	
	/**
	 * 计算打印图片的价格
	 */
	@Override
	public double getImgPrice(String name) {
		double price = p.get(0).getPrice();
		return Double.parseDouble(df.format(price));
	}
	
	/**
	 * 计算打印word文件的价格
	 */
	@Override
	public double getWordPrice(String name) {
		String[] str = name.split("\\.");
		String kind = "."+str[1];	//获得文件的后缀名
		double price = p.get(1).getPrice();
		int pages = 0;
		
		if(kind.equals(".doc")){
			try {
				pages = new GetFilePages().getPages4DOC(name);//拿到docWord文件的页数
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		if(kind.equals(".docx")){
			try {
				pages = new GetFilePages().getPages4DOCX(name);//拿到docxWord文档页数
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return Double.parseDouble(df.format(price*pages));
		
	}

	/**
	 * 计算打印PDF的价格
	 */
	@Override
	public double getPDFPrice(String name) {
		double price = p.get(2).getPrice();
		int pages = 0;
		try {
			pages = new GetFilePages().getPDFPages(name);//拿到PDF的页数
		} catch (IOException e) {
			e.printStackTrace();
		}
		return Double.parseDouble(df.format(price*pages));
	}

}
